package com.seed.application.web.impl;

import cn.hutool.core.util.StrUtil;
import com.seed.application.web.WebContext;
import com.seed.core.enumeration.Direction;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Component
public class DefaultWebContext implements WebContext {

    private final HttpServletRequest request;

    public DefaultWebContext(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    @Override
    public Map<String, String> getSearch() {
        return getSearchStartingWith("search_");
    }

    @Override
    public Map<String, Direction> getOrder() {
        return getOrderStartingWith("order_");
    }

    @Override
    public String getKeyword() {
        return request.getParameter("keyword");
    }

    @Override
    public List<List<String>> getDataPermis() {
        return Collections.emptyList();
    }

    @Override
    public LinkedHashMap<String, String> getFieldPermis() {
        LinkedHashMap<String, String> ret = new LinkedHashMap<>();
        ret.put("name", "=加1");

        return ret;
    }

    @Override
    public String getSeedName() {
        return (String) request.getAttribute("seedName");
    }

    @Override
    public String getActionName() {
        return (String) request.getAttribute("actionName");
    }


    /**
     * 取得带相同前缀的Request Parameters, copy from spring WebUtils.
     *
     * 返回的结果的Parameter名已去除前缀.
     */
    private Map<String, String> getSearchStartingWith(String prefix) {
        Enumeration<String> paramNames = request.getParameterNames();
        Map<String, String> ret = new HashMap<>();
        if (prefix == null) prefix = "";

        while ((paramNames != null) && paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            if ("".equals(prefix) || paramName.startsWith(prefix)) {
                String unprefixed = paramName.substring(prefix.length());
                String[] values = request.getParameterValues(paramName);
                if ((values == null) || (values.length == 0)) {
                    // Do nothing, no values found at all.
                } else if (values.length > 1) {
                    ret.put(unprefixed, StrUtil.join(",", List.of(values)));
                } else {
                    ret.put(unprefixed, values[0]);
                }
            }
        }

        return ret;
    }

    private Map<String, Direction> getOrderStartingWith(String prefix) {
        Enumeration<String> paramNames = request.getParameterNames();
        Map<String, Direction> ret = new HashMap<>();
        if (prefix == null) prefix = "";

        while ((paramNames != null) && paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            if ("".equals(prefix) || paramName.startsWith(prefix)) {
                String unprefixed = paramName.substring(prefix.length());
                String[] values = request.getParameterValues(paramName);
                if ((values == null) || (values.length == 0)) {
                    ret.put(unprefixed, Direction.asc);
                } else {
                    ret.put(unprefixed, Direction.valueFrom(values[0]));
                }
            }
        }

        return ret;
    }
}
